package com.gec.auction.tools;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:gec
 * @Date:2022/4/12
 * @Description:code
 * @version:1.0
 */
@Configuration
public class MyExceptionHandler  implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        AuctionCustomerException auctionCustomerException=null;
        //判断异常类型是否是属于自定义异常类型
        if(e instanceof AuctionCustomerException){
            auctionCustomerException = (AuctionCustomerException) e;
        }else{
            //其它异常类型
            auctionCustomerException = new AuctionCustomerException("未知异常.....");
        }
        //将错误信息提取出来
        modelAndView.addObject("errorMsg",auctionCustomerException.getMessage());
        //跳转到错误页面
        modelAndView.setViewName("errorPage");
        return modelAndView;
    }
}